package ant;

import java.util.ArrayList;
import java.util.Iterator;


import world.*;

import org.red5.io.amf3.IDataOutput;

import ant.properties.Property;


/**
 * @author PMOHTACH
 *
 */
public class Colony extends WOAObject{
	private ArrayList<Ant> ants = new ArrayList<Ant>();;
	public Queen queen = null;
	
	float food_storage = 300.0f;


	public Colony(Queen q, AntWorld w, Position pos)
	{	
		super(w, pos);
		this.world = w;
		this.setPos(pos);
		ArrayList<Colony> c = this.world.getColonys();
		c.add(this);
		

		this.world.addColonys_screen(pos.get_map(), this);
		type = "colony";
		
		adn.set("size", new Property("size", 10.f));
		
		// add the colony shape to the physic world
		setMyBody(world.antBox.create_colony(pos.getVec2()));

		this.world.log.debug("Colony has been created.");
		
		setQueen(q);		
	
	}	
	

	/**
	 * @return colony's queen
	 */
	public Queen getQueen() {
		return queen;
	}

	
	/**
	 * @param set the colony's queen
	 */
	public void setQueen(Queen queen) {
		this.queen = queen;
	}

	public ArrayList<Ant> getAnts() {
		return ants;
	}

	public void setAnts(ArrayList<Ant> ants) {
		this.ants = ants;
	}
	
	public void life()
	{
		try
		{
			if (getQueen() != null)	getQueen().life();		
			Iterator<Ant> i = getAnts().iterator();
			while (i.hasNext())
			{
				Ant a = i.next();
				a.life();
			}
			//if ((Math.random() * 100) > 69)
			{
				//antCreate();	
			}
			//world.log.debug(id + " size : " + adn.getCurrent("size") );
			world.log.debug(id + " pos : " + pos.x + "," + pos.y + ", size : "  +  adn.getCurrent("size"));
					
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public IDataOutput Export(IDataOutput output)
	{
		output.writeObject(getPos());
		return output;
	}

	
	public Ant antCreate()
	{
		// create the ant
		Ant a = new Ant(this);
		
		// add the ant to the ants list of the colony
		getAnts().add(a);
		
		return a;
	}


}
